package com.vcg.neo4j.spring;

import com.vcg.neo4j.spring.bean.UserService;
import com.vcg.neo4j.spring.bean.UserServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * Created by wuyu on 2016/8/4.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@SpringBootApplication
public class BeanPostProcessFactoryTest {

    @Autowired
    ConfigurableListableBeanFactory factory;

    @Autowired
    AutowiredAnnotationBeanPostProcessor autowiredAnnoBean;

    @Autowired
    AnnotationConfigApplicationContext annoApplication;

    @Test
    //第一种
    public void testAnnoApplicationRegistry() {
        annoApplication.register(UserServiceImpl.class);
        UserService userService = factory.getBean(UserService.class);
        boolean wuyu = userService.hasRole("wuyu");
        System.err.println(wuyu);
    }

    @Test
    public void testGetBean(){

    }


    @Test
    //第二种
    public void testFactoryRegistry() {
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) factory;
        UserService userService = new UserServiceImpl();
        autowiredAnnoBean.processInjection(userService);
        factory.registerSingleton("userService", userService);
        UserService userService2 = factory.getBean(UserService.class);
        System.err.println(userService2);
        System.err.println(userService2.hasRole("wuyu"));
        defaultListableBeanFactory.destroySingleton("userService");
        UserService userService3 = factory.getBean(UserService.class);
        System.err.println(userService3);
    }

    @Test
    //第三种
    public void testFactoryRegstry2() {
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(UserServiceImpl.class)
                .setScope("prototype")
                .getBeanDefinition();
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) factory;
        defaultListableBeanFactory.registerBeanDefinition("userService",beanDefinition);
        UserService userService = defaultListableBeanFactory.getBean(UserService.class);
        System.err.println(userService);
        //销毁bean
        defaultListableBeanFactory.destroyBean(userService);
        UserService userservice2 = defaultListableBeanFactory.getBean(UserService.class);
        System.err.println(userservice2);
        System.err.println(userService==userservice2);
        boolean wuyu = userService.hasRole("wuyu");
        //System.err.println(wuyu);


    }

    @WebFilter(filterName="testFilter",urlPatterns="/*")
    @Component
    public class testFilter implements Filter {

        @Autowired
        UserService userService;

        @Override
        public void destroy() {
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        }

        @Override
        public void init(FilterConfig config) throws ServletException {
        }

    }

}
